home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 163_01 / cc51.c < prev    next >
Text File  |  1990-11-20  |  18KB  |  666 lines

  1. /*
  2. ** peephole optimizer
  3. **
  4. ** basically a simple text replacement processor
  5. */
  6.  
  7. char nooptimize = 0; /* this flag disables optimization if set (by debug) */
  8.  
  9. peephole(ptr) char *ptr; {
  10.   while(*ptr) {
  11.     char *ptr1;
  12.     ptr1 = ptr; /* save for later compare */
  13.     /*
  14.     ** if optimization is disabled, skip everything
  15.     ** all patterns start with tab, so look for one
  16.     */
  17.     while(*ptr && (*ptr != '\t'|| nooptimize)) cout(*ptr++,output);
  18.     if(streq(ptr, "\tLEA AX,[BP]")) ptr = optim1(ptr, 12);
  19.     if(streq(ptr, "\tMOV AX,1\n")) ptr = optim3(ptr, 10);
  20.     if(streq(ptr, "\tMOV AX,")) ptr = optim4(ptr, 8);
  21.     if(streq(ptr, "\tPUSH AX\n\tMOV AX,")) ptr = optim5(ptr, 17);
  22.     if(streq(ptr, "\tPUSH AX\n\tLEA AX,[BP]")) ptr = optim6(ptr, 21);
  23.     if(streq(ptr, "\tMOV BX,DX\n\tPOP BX\n")) ptr = optim7(ptr, 19);
  24.     if(streq(ptr, "\tMOV BX,")) ptr = optim8(ptr, 8);
  25.     if(streq(ptr, "\tJNZ $+5\n")) ptr = optim9(ptr+9, "JNZ $+");
  26.     if(streq(ptr, "\tJZ $+5\n")) ptr = optim9(ptr+8, "JZ $+");
  27.     /* additional optimizing logic goes here  */
  28.     if(ptr == ptr1) cout(*ptr++, output);
  29.     }
  30.   }
  31.  
  32. /*
  33. ** optimize strings beginning "\tLEA AX,[BP]"
  34. */
  35. optim1(ptr, bump) char *ptr; int bump; {
  36.   char offset[20], *ptr1, *ptr2;
  37.   ptr1 = ptr + bump; /* skip text already recognized */
  38.   ptr2 = offset;
  39.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save offset from LEA */
  40.   *--ptr2 = 0;
  41.   ++bump;
  42.   ptr2 = ptr; /* save for compare */
  43.   if(streq(ptr1, "\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"))
  44.     ptr = optim11(ptr, bump+35, offset);
  45.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n"))
  46.     ptr = optim12(ptr, bump+24, offset);
  47.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  48.     ot("MOV AL,[BP]");
  49.     outstr(offset);
  50.     outstr(" ;optim1 - 1");
  51.     nl();
  52.     ptr = ptr1 + 24;
  53.     }
  54.   else if(streq(ptr1, "\tPUSH AX\n\tLEA AX,[BP]"))
  55.     ptr = optim13(ptr, bump+21, offset);
  56.   else if(streq(ptr1, "\tPUSH AX\n\tMOV AX,"))
  57.     ptr = optim14(ptr, bump+17, offset);
  58.   else if(streq(ptr1, "\tPUSH AX\n\tMOV SI,AX\n"))
  59.     ptr = optim15(ptr, bump+20, offset);
  60.   return ptr;
  61.   }
  62.  
  63. /*
  64. ** optimize strings beginning
  65. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  66. */
  67. optim11(ptr, bump, offset) char *ptr, offset[]; int bump; {
  68.   char *ptr1, *ptr2;
  69.   ptr1 = ptr + bump; /* skip text already recognized */
  70.   ptr2 = ptr; /* save for compare */
  71.   if(streq(ptr1, "\tINC AX\n\tMOV [BX],AX\n"))
  72.     ptr = optim111(ptr, bump+21, offset);
  73.   else if(streq(ptr1, "\tDEC AX\n\tMOV [BX],AX\n"))
  74.     ptr = optim112(ptr, bump+21, offset);
  75.   return ptr;
  76.   }
  77.  
  78. /*
  79. ** optimize strings beginning
  80. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  81. **   "\tINC AX\n\tMOV [BX],AX\n"
  82. */
  83. optim111(ptr, bump, offset) char *ptr, offset[]; int bump; {
  84.   char *ptr1, *ptr2;
  85.   ptr1 = ptr + bump; /* skip text already recognized */
  86.   ptr2 = ptr; /* save for compare */
  87.   if(streq(ptr1, "\tDEC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  88.     if(ptr1[32]) { /* if not end of expression */
  89.       ot("MOV BX,[BP]");
  90.       outstr(offset);
  91.       nl();
  92.       ot("INC WORD PTR [BP]");
  93.       outstr(offset);
  94.       nl();
  95.       ol("MOV AL,[BX] ;optim111 - 1");
  96.       ptr = ptr1 + 32;
  97.       }
  98.     else
  99.       ptr = opti1111(ptr, bump+32, offset);
  100.     }
  101.   else if(streq(ptr1, "\tDEC AX\n")) {
  102.     if(ptr1[8]) { /* if not end of expression */
  103.       ot("MOV AX,[BP]");
  104.       outstr(offset);
  105.       nl();
  106.       ot("INC WORD PTR [BP]");
  107.       outstr(offset);
  108.       outstr(" ;optim111 - 2");
  109.       nl();
  110.       ptr = ptr1 + 8;
  111.       }
  112.     else
  113.       ptr = opti1111(ptr, bump+8, offset);
  114.     }
  115.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  116.     if(ptr1[8]) { /* if not end of expression */
  117.       ot("INC WORD PTR [BP]");
  118.       outstr(offset);
  119.       nl();
  120.       ot("MOV BX,[BP]");
  121.       outstr(offset);
  122.       nl();
  123.       ol("MOV AL,[BX] ;optim111 - 3");
  124.       ptr = ptr1 + 24;
  125.       }
  126.     else
  127.       ptr = opti1111(ptr, bump+24, offset);
  128.     }
  129.   else {
  130.     if(ptr1[0]) { /* if not end of expression */
  131.       ot("INC WORD PTR [BP]");
  132.       outstr(offset);
  133.       nl();
  134.       ot("MOV AX,[BP]");
  135.       outstr(offset);
  136.       outstr(" ;optim111 - 4");
  137.       nl();
  138.       ptr = ptr1;
  139.       }
  140.     else
  141.       ptr = opti1111(ptr, bump, offset);
  142.     }
  143.   return ptr;
  144.   }
  145.  
  146. /*
  147. ** optimize "++" operators on simple auto variables with no destination
  148. */
  149. opti1111(ptr, bump, offset) char *ptr, offset[]; int bump; {
  150.   ot("INC WORD PTR [BP]");
  151.   outstr(offset);
  152.   outstr(" ;opti1111 - 1");
  153.   nl();
  154.   return ptr + bump;
  155.   }
  156.  
  157. /*
  158. ** optimize strings beginning
  159. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  160. **   "\tDEC AX\n\tMOV [BX],AX\n"
  161. */
  162. optim112(ptr, bump, offset) char *ptr, offset[]; int bump; {
  163.   char *ptr1, *ptr2;
  164.   ptr1 = ptr + bump; /* skip text already recognized */
  165.   ptr2 = ptr; /* save for compare */
  166.   if(streq(ptr1, "\tINC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  167.     if(ptr1[32]) { /* if not end of expression */
  168.       ot("MOV BX,[BP]");
  169.       outstr(offset);
  170.       nl();
  171.       ot("DEC WORD PTR [BP]");
  172.       outstr(offset);
  173.       nl();
  174.       ol("MOV AL,[BX] ;optim112 - 1");
  175.       ptr = ptr1 + 32;
  176.       }
  177.     else
  178.       ptr = opti1121(ptr, bump+32, offset);
  179.     }
  180.   else if(streq(ptr1, "\tINC AX\n")) {
  181.     if(ptr1[8]) { /* if not end of expression */
  182.     ot("MOV AX,[BP]");
  183.     outstr(offset);
  184.     nl();
  185.     ot("DEC WORD PTR [BP]");
  186.     outstr(offset);
  187.     outstr(" ;optim112 - 2");
  188.     nl();
  189.     ptr = ptr1 + 8;
  190.       }
  191.     else
  192.       ptr = opti1121(ptr, bump+8, offset);
  193.     }
  194.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  195.     if(ptr1[24]) { /* if not end of expression */
  196.       ot("DEC WORD PTR [BP]");
  197.       outstr(offset);
  198.       nl();
  199.       ot("MOV BX,[BP]");
  200.       outstr(offset);
  201.       nl();
  202.       ol("MOV AL,[BX] ;optim112 - 3");
  203.       ptr = ptr1 + 24;
  204.       }
  205.     else
  206.       ptr = opti1121(ptr, bump+24, offset);
  207.     }
  208.   else {
  209.     if(ptr1[0]) { /* if not end of expression */
  210.       ot("DEC WORD PTR [BP]");
  211.       outstr(offset);
  212.       nl();
  213.       ot("MOV AX,[BP]");
  214.       outstr(offset);
  215.       outstr(" ;optim112 - 4");
  216.       nl();
  217.       ptr = ptr1;
  218.       }
  219.     else
  220.       ptr = opti1121(ptr, bump, offset);
  221.     }
  222.   return ptr;
  223.   }
  224.  
  225. /*
  226. ** optimize "--" operators on simple auto variables with no destination
  227. */
  228. opti1121(ptr, bump, offset) char *ptr, offset[]; int bump; {
  229.   ot("DEC WORD PTR [BP]");
  230.   outstr(offset);
  231.   outstr(" ;opti1121 - 1");
  232.   nl();
  233.   return ptr + bump;
  234.   }
  235.  
  236. /*
  237. ** optimize strings beginning
  238. **   "\tLEA AX,[BP]xxxx\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  239. */
  240. optim12(ptr, bump, offset) char *ptr, offset[]; int bump; {
  241.   char *ptr1, *ptr2;
  242.   ptr1 = ptr + bump; /* skip text already recognized */
  243.   ptr2 = ptr; /* save for compare */
  244.   if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n")) {
  245.     ot("MOV SI,[BP]");
  246.     outstr(offset);
  247.     nl();
  248.     ol("MOV AX,[SI] ;optim12 - 1");
  249.     ptr = ptr1 + 24;
  250.     }
  251.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n\tCBW\n\tAND AX,AX\n")) {
  252.     ot("MOV SI,[BP]");
  253.     outstr(offset);
  254.     nl();
  255.     ol("CMP BYTE PTR [SI],0 ;optim12 - 2");
  256.     ptr = ptr1 + 40;
  257.     }
  258.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  259.     ot("MOV SI,[BP]");
  260.     outstr(offset);
  261.     nl();
  262.     ol("MOV AL,[SI] ;optim12 - 3");
  263.     ptr = ptr1 + 24;
  264.     }
  265.   else {
  266.     ot("MOV AX,[BP]");
  267.     outstr(offset);
  268.     outstr(" ;optim12 - 4");
  269.     nl();
  270.     ptr = ptr1;
  271.     }
  272.   return ptr;
  273.   }
  274.  
  275. /*
  276. ** optimize strings beginning "\tLEA AX,[BP]xxxx\n\tPUSH AX\n\tLEA AX,[BP]"
  277. */
  278. optim13(ptr, bump, offset) char *ptr, offset[]; int bump; {
  279.   char offset2[20], *ptr1, *ptr2;
  280.   ptr1 = ptr + bump; /* skip text already recognized */
  281.   ptr2 = offset2;
  282.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from LEA */
  283.   *--ptr2 = 0;
  284.   ++bump;
  285.   ptr2 = ptr; /* save for compare */
  286.   if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n\tPOP BX\n\tMOV [BX],AX\n")) {
  287.     ot("MOV AX,[BP]");
  288.     outstr(offset2);
  289.     nl();
  290.     ot("MOV [BP]");
  291.